package com.acme.concurrent.indexer;


import java.io.File;
import java.io.FileFilter;

/**
 * ProducerConsumer
 * <p/>
 * Producer and consumer tasks in a desktop search application
 *
 * @author Brian Goetz and Tim Peierls
 */
public class ProducerConsumer {

    private static final int N_CONSUMERS = 6; //Runtime.getRuntime().availableProcessors();

    public static void startIndexing(File[] roots) {

        FileFilter filter = new FileFilter() {
            public boolean accept(File file) {
                return true;
            }
        };

        FileQueuePool fileQueuePool = new FileQueuePool(N_CONSUMERS, roots.length);

        for (File root : roots)
            new Thread(new FileCrawler(fileQueuePool, filter, root)).start();

        for (int i = 0; i < N_CONSUMERS; i++)
            new Thread(new FileIndexer(fileQueuePool, i)).start();

/*

        try {
            fileQueuePool.awaitFinalization();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
*/
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        fileQueuePool.shutdown();
    }
}